/*
 * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.amazonaws.demo;

import java.util.List;

import com.amazonaws.demo.R;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public abstract class CustomListActivity extends AlertActivity {

	protected ListView itemList;
	protected TextView loadingText;
	protected Button moreButton;
	protected ArrayAdapter<String> itemListAdapter;

	public static final int LEFT = 0;
	public static final int CENTER = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_list);
		itemList = (ListView) findViewById(R.id.item_list_view);
		loadingText = (TextView) findViewById(R.id.item_list_loading_text);
		moreButton = (Button) findViewById(R.id.item_list_more_button);
	}

	protected void updateUi(String[] list, String successMessage) {
		updateUi(list, successMessage, CENTER);
	}

	protected void updateUi(List<String> list, String successMessage) {
		updateUi(list, successMessage, CENTER);
	}

	protected void updateUi(List<String> list, String successMessage,
			int justify) {
		loadingText.setText(successMessage);
		loadingText.setTextSize(16);
		if (justify == LEFT) {
			itemListAdapter = new ArrayAdapter<String>(this, R.layout.row_left,
					list);
		} else if (justify == CENTER) {
			itemListAdapter = new ArrayAdapter<String>(this, R.layout.row, list);
		}
		itemList.setAdapter(itemListAdapter);
		itemListAdapter.notifyDataSetChanged();
		wireOnListClick();
	}

	protected void updateUi(String[] list, String successMessage, int justify) {
		loadingText.setText(successMessage);
		loadingText.setTextSize(16);
		if (justify == LEFT) {
			itemListAdapter = new ArrayAdapter<String>(this, R.layout.row_left);
		} else if (justify == CENTER) {
			itemListAdapter = new ArrayAdapter<String>(this, R.layout.row);
		}
		itemList.setAdapter(itemListAdapter);
		for (String item : list) {
			itemListAdapter.add(item);
		}
		itemListAdapter.notifyDataSetChanged();
		wireOnListClick();
	}

	protected void updateList(String[] itemNameList) {
		for (String item : itemNameList) {
			itemListAdapter.add(item);
		}
		itemListAdapter.notifyDataSetChanged();
		wireOnListClick();
	}

	protected void updateList(List<String> itemNameList) {
		if (itemNameList.size() == 0) {
			disablePagination();
		} else {
			enablePagination();
			for (String item : itemNameList) {
				itemListAdapter.add(item);
			}
			itemListAdapter.notifyDataSetChanged();
			wireOnListClick();
		}
	}

	protected void startPopulateList() {
		obtainListItems();
	}

	protected void getMoreItems() {
		moreButton.setOnClickListener(null);
		obtainMoreItems();
	}

	protected abstract void obtainListItems();

	protected void obtainMoreItems() {
		return;
	}

	public ListView getItemList() {
		return itemList;
	}

	public void enablePagination() {
		moreButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getMoreItems();
			}
		});
		moreButton.setVisibility(View.VISIBLE);
	}

	public void disablePagination() {
		moreButton.setOnClickListener(null);
		moreButton.setVisibility(View.INVISIBLE);
	}

	protected void wireOnListClick() {
		return;
	}

}
